package com.cskaoyan.service;

import com.cskaoyan.bean.bo.AdvertiseBo;
import com.cskaoyan.bean.bo.AdvertiseUpdateBo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.AdvertisePo;
import com.cskaoyan.bean.vo.AdvertiseListVo;
import com.cskaoyan.bean.vo.AdvertiseUpdateVo;
import com.cskaoyan.bean.vo.AdvertiseVo;
import com.cskaoyan.mapper.AdvertiseMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * /admin/ad/list?page=1&limit=20&sort=add_time&order=desc;
 *
 * @author: 陈慧颖
 * @time: Created by chy on 2022/6/3 20:27
 */
@Service
public class AdvertiseServiceImpl implements AdvertiseService {
    @Autowired  //sql 打交道的事情 mapper

    AdvertiseMapper advertiseMapper;

    /**
     * 传入参数,使用对象接收;
     *  注意: 依赖于:/admin/storage/create; 该功能需要lsj 实现; todo-> lsj实现, 等待兼容 linux;
     * @param advertiseBo
     * @return
     */
    @Override
    public AdvertiseVo create(AdvertiseBo advertiseBo) {
        System.out.println("advertiseBo = " + advertiseBo);
        // 将 VXCollectAddOrDeleteBo 对象转为 Po 对象;
        AdvertisePo advertisePo = new AdvertisePo();
        advertisePo.setContent(advertiseBo.getContent());
        /**
         * 添加时 ;  前端给带了 enabled 值 boolean ;   java 代码创建了 tempEnabled用以与数据库交互, 但可以自动转化;
         */
        Integer tempEnabled = advertiseBo.getEnabled() == true ? 1 : 0;
        advertisePo.setEnabled(tempEnabled);
        advertisePo.setLink(advertiseBo.getLink()==null?"":advertiseBo.getLink());
        advertisePo.setName(advertiseBo.getName());
        advertisePo.setPosition(advertiseBo.getPosition());
        advertisePo.setUrl(advertiseBo.getUrl());
        advertisePo.setAddTime(new Date());
        advertisePo.setUpdateTime(new Date());
        advertisePo.setId(null);
        /**
         * 向 Po 对象补充 deleted 属性;
         * 默认是  0;
         */
        advertisePo.setDeleted(0);
        System.out.println("advertisePo = " + advertisePo);
        // 向数据里插入;
       int count =   advertiseMapper.insertOne(advertisePo);
         // 根据 mybatis 获取自增 id;
        Integer id = advertisePo.getId();
        AdvertiseVo advertiseVo = new AdvertiseVo();
        advertiseVo.setId(id);// 必须配合 mybatis 的 useGeneratedKeys 属性;
        advertiseVo.setAddTime(advertisePo.getAddTime());
        advertiseVo.setEnabled(advertiseBo.getEnabled()); // 复用了 VXCollectAddOrDeleteBo 对象的 Enabled;
        advertiseVo.setContent(advertisePo.getContent());
        advertiseVo.setLink(advertisePo.getLink());
        advertiseVo.setName(advertisePo.getName());
        advertiseVo.setPosition(advertisePo.getPosition());
        advertiseVo.setUpdateTime(advertisePo.getUpdateTime());
        advertiseVo.setUrl(advertisePo.getUrl());
        advertiseVo.setDeleted(advertisePo.getDeleted()==0?false:true);

        return advertiseVo;
    }

    /**
     * 根据客户端传来参数封装到advertiseUpdateBo中,
     * 在 servcie 层将 VXCollectAddOrDeleteBo->PO;  将 PO 传递给 Dao 层或 mapper 层; 同时 mapper 层返回 Vo 对象;继续将 Vo 对象上传到 controller 层;
     * @param advertiseUpdateBo
     * @return
     */
    @Override
    public AdvertiseUpdateVo updateOneAdvertise( AdvertiseUpdateBo advertiseUpdateBo) {
        // VXCollectAddOrDeleteBo->Po ; VXCollectAddOrDeleteBo 和 Po 一模一样;
        System.out.println("enabled:  "+advertiseUpdateBo.getEnabled());

        AdvertisePo advertisePo = new AdvertisePo();
        advertisePo.setId(advertiseUpdateBo.getId());
        advertisePo.setName(advertiseUpdateBo.getName());
        advertisePo.setLink(advertiseUpdateBo.getLink());
        advertisePo.setUrl(advertiseUpdateBo.getUrl());
        advertisePo.setPosition(advertiseUpdateBo.getPosition());
        advertisePo.setContent(advertiseUpdateBo.getContent());
        // 前端没有将该advertiseUpdateBo里的'是否启动'值传递而来;
        /**
         * 有可能是 我在添加时没有给到该'是否启动'值;
         */
        Integer tempEnabled = advertiseUpdateBo.getEnabled()==true?1:0;

        advertisePo.setEnabled(tempEnabled);
        advertisePo.setAddTime(advertiseUpdateBo.getAddTime());// advertiseUpdateBo.getAddTime()  是前一次
        advertisePo.setUpdateTime(new Date()); // advertiseUpdateBo.getUpdateTime()  即时 update 的时间
        Integer  tempDeleted  = advertiseUpdateBo.getDeleted()==true?1:0;
        advertisePo.setDeleted(tempDeleted);
        // PO 对象 advertisePo
        // mapper 操作 ; update 操作返回afftectedRows;
        int count =    advertiseMapper.updateOneAdvertise(advertisePo);

        // 返回 Vo; 此时, Vo 与 VXCollectAddOrDeleteBo 形式上完全一致 ,但是 updateTime 需要注意:
        AdvertiseUpdateVo advertiseUpdateVo = new AdvertiseUpdateVo();
        advertiseUpdateVo.setId(advertisePo.getId());
        advertiseUpdateVo.setName(advertisePo.getName());
        advertiseUpdateVo.setLink(advertisePo.getLink());
        advertiseUpdateVo.setPosition(advertisePo.getPosition());
        advertiseUpdateVo.setContent(advertisePo.getContent());
        advertiseUpdateVo.setEnabled(advertiseUpdateBo.getEnabled()); // 无法复用 Po 里面的Enabled 和 Deleted;
        advertiseUpdateVo.setAddTime(advertisePo.getAddTime()); // addTime 是上一次的;
        advertiseUpdateVo.setUpdateTime(advertisePo.getUpdateTime()); // 获取到 Po 对象的即时插入时间;
        advertiseUpdateVo.setDeleted(advertiseUpdateBo.getDeleted());// 无法复用 Po 里面的 Deleted;
        return advertiseUpdateVo;
    }


    // @Override
    // public void deleteOne(AdvertiseUpdateBo advertiseUpdateBo) {
    //     // VXCollectAddOrDeleteBo->PO; 但是实现没有必要转;
    //     // dao 或 mapper 操作数据库;
    //   int count =    advertiseMapper.deleteOne(advertiseUpdateBo);
    //     return ;
    // }
    /**
     * delete方法;
     * 修订: 2022年06月06日09:11:40  chy  假删除 
     * @param advertiseUpdateBo
     * @return
     */
    @Override
    public void deleteOne(AdvertiseUpdateBo advertiseUpdateBo) {
        // VXCollectAddOrDeleteBo->PO; 但是实现没有必要转;
        // dao 或 mapper 操作数据库;
        int count =    advertiseMapper.deleteOne(advertiseUpdateBo);
        return ;
    }




    @Override
    public AdvertiseListVo listAll(BaseParam baseParam,String name ,String content) {
        // 1. 开启分页
        PageHelper.startPage(baseParam.getPage(),baseParam.getLimit());
        // 从数据库查询出 advertisePo 集合;
        List<AdvertisePo> advertisePos = advertiseMapper.queryAll(baseParam,name, content);
//
        PageInfo pageInfo = new PageInfo(advertisePos);
        long total = pageInfo.getTotal();
        int pages1 = pageInfo.getPages();

        // 将该集合转化为 advertiseVo 集合;
        ArrayList<AdvertiseVo> advertiseVos = new ArrayList<>();
        for (AdvertisePo advertisePo : advertisePos) {
            AdvertiseVo advertiseVo = new AdvertiseVo();

            advertiseVo.setId(advertisePo.getId());
            advertiseVo.setName(advertisePo.getName());
            advertiseVo.setLink(advertisePo.getLink());
            advertiseVo.setUrl(advertisePo.getUrl());
            advertiseVo.setPosition(advertisePo.getPosition());
            advertiseVo.setContent(advertisePo.getContent());

            advertiseVo.setAddTime(advertisePo.getAddTime());
            advertiseVo.setUpdateTime(advertisePo.getUpdateTime());
            Boolean tempDeleted = advertisePo.getDeleted() == 1 ? true : false;
            Boolean tempEnabled = advertisePo.getEnabled() == 1 ? true : false;
            advertiseVo.setDeleted(tempDeleted);
            advertiseVo.setEnabled(tempEnabled);

            advertiseVos.add(advertiseVo);
        }
        // 将advertiseVo 集合给到AdvertiseListVo;
        AdvertiseListVo advertiseListVo = new AdvertiseListVo();
        advertiseListVo.setList(advertiseVos);
        // 映射其余参数
        advertiseListVo.setLimit(baseParam.getLimit());
        advertiseListVo.setPage(baseParam.getPage());
        // 从数据库获取到 pages 和 total; 须经过计算给到advertiseListVo;
        advertiseListVo.setTotal((int) total);
        advertiseListVo.setPages(pages1);
        // 返回AdvertiseListVo给 controller;
        return advertiseListVo;
        // 查询广告总数量
        // int count = advertiseMapper.queryAllCount();
        // // 根据每一页数量与总量计算 pages; 总页码;
        // int pages = 0;
        // try {
        //     // if (baseParam.getLimit()!=null && baseParam.getLimit()!=0){
        //     //
        //     // }
        //     pages = count / baseParam.getLimit();
        // } catch (ArithmeticException e) {
        //     // 若客户端传来 0; 则应该抛出异常;
        //     e.printStackTrace();
        // }
    }
}
